package br.com.Orange.dao.cliente;

import java.util.Collection;

import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.Orange.controller.exception.ClienteInexistenteException;
import br.com.Orange.hibernate.HibernateFactory;
import br.com.Orange.model.Cliente;
import br.com.Orange.util.dados.exception.RepositorioVazioException;

public class RepositorioClienteBDR implements RepositorioCliente{

	@Override
	public void atualizar(Cliente cliente) throws ClienteInexistenteException {
		
		Session session = HibernateFactory.getSession();

		session.update(cliente);
		

		
	}

	@Override
	public boolean existe(String CPF) {
		Session session = HibernateFactory.getSession();
		Cliente cliente = (Cliente)session.get(Cliente.class, CPF);
		session.close();
		if (cliente != null){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public void inserir(Cliente cliente) {
		
		Session session = HibernateFactory.getSession();
		Transaction tx = session.beginTransaction();
		session.save(cliente);
		tx.commit();
		session.close();
		
	}

	@Override
	public Collection<Cliente> listar() throws RepositorioVazioException {

		

		
		return null;
	}

	@Override
	public Cliente procurar(String CPF) throws ClienteInexistenteException {	
		Session session = HibernateFactory.getSession();
		Cliente cliente = (Cliente)session.get(Cliente.class, CPF);
		session.close();
		if (cliente != null){
			return cliente;
		}else{
			throw new ClienteInexistenteException(CPF);
		}
		
	}

	@Override
	public void remover(String CPF) throws ClienteInexistenteException {
		Session session = HibernateFactory.getSession();
		Cliente cliente = (Cliente)session.get(Cliente.class, CPF);
		if (cliente != null){
			Transaction tx = session.beginTransaction();
			session.delete(cliente);
			tx.commit();
			session.close();
		}else{
			session.close();
			throw new ClienteInexistenteException(CPF);
		}	
	}
	
	

}
